package xyz.lovegu.server.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.lovegu.server.bus.dos.Leave;
import xyz.lovegu.server.bus.mapper.LeaveMapper;
import xyz.lovegu.server.bus.service.LeaveService;
import xyz.lovegu.server.bus.vos.LeaveVo;
import xyz.lovegu.server.sys.common.DataGridView;
import xyz.lovegu.server.sys.common.ResultObj;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

/**
 * @author 老顾
 * @title: LeaveServiceImpl
 * @projectName campus-prevention-system
 * @email: 1437594522@qq.com
 * @date 2022/5/10 17:36
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements LeaveService {


    /**
     * 增加请假申请表
     *
     * @param leaveVo
     * @return
     */
    @Override
    public ResultObj addLeave(LeaveVo leaveVo) {
        try {
            Leave leave = new Leave();
            BeanUtils.copyProperties(leaveVo,leave);
            this.save(leave);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 批量删除请假表
     *
     * @param leaveVo
     * @return
     */
    @Override
    public ResultObj deleteBatchLeave(LeaveVo leaveVo) {
        try {
            Collection<Serializable> list = new ArrayList<>(Arrays.asList(leaveVo.getIds()));
            this.removeByIds(list);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }

    }

    /**
     * 修改请假表审批状态
     *
     * @param leaveVo
     * @return
     */
    @Override
    public ResultObj updateLeaveStatus(LeaveVo leaveVo) {
        Leave leave = this.getById(leaveVo.getId());
        if (leave==null){
            return ResultObj.LEAVE_NOT_EXIST;
        }
        try {
            leave.setUserId(leaveVo.getUserId());
            leave.setStatus(leaveVo.getStatus());
            leave.setUpdateTime(new Date());
            this.updateById(leave);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 根据班级查询所有请假表
     *
     * @param leaveVo
     * @return
     */
    @Override
    public Object getLeaveByClasses(LeaveVo leaveVo) {
        IPage<Leave> page = new Page<>(leaveVo.getPage(),leaveVo.getLimit());
        LambdaQueryWrapper<Leave> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StrUtil.isNotBlank(leaveVo.getClasses()),Leave::getClasses,leaveVo.getClasses());
        lambdaQueryWrapper.eq(leaveVo.getStartTime()!=null,Leave::getCreateTime,leaveVo.getStartTime());
        lambdaQueryWrapper.eq(leaveVo.getEndTime()!=null,Leave::getCreateTime,leaveVo.getEndTime());
        lambdaQueryWrapper.orderByDesc(Leave::getCreateTime);
        this.page(page,lambdaQueryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }
}
